<?php

/**
 * This file is part of geekPWS.
 * 
 * Copyright (C) Iain Learmonth 2010.
 *
 * geekPWS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * geekPWS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with geekPWS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @package geekPWS
 * @subpackage CoreBlogSystem
 */

/**
 * This class represents a blog post in the database.
 * 
 * @author Iain Learmonth <adapa@hotmail.co.uk>
 * @version 0.1
 */
class BlogPost
{
	
	/**
	 * The post ID
	 * 
	 * @var Integer
	 */
	private $id;
	
	/**
	 * The post title
	 * 
	 * @var String
	 */
	private $title;
	
	/**
	 * The post creation time
	 * 
	 * @var String
	 */
	private $ctime;
	
	/**
	 * The post last modified time
	 * 
	 * @var String 
	 */
	private $mtime;
	
	/**
	 * The count of page views for the post
	 * 
	 * @var Integer
	 */
	private $views;
	
	/**
	 * The content of the post
	 * 
	 * @var String
	 */
	private $content;
	
	/**
	 * Constructor for the class BlogPost. Populates
	 * fields with values from the database. 
	 * 
	 * By passing an ID value of zero, no post data
	 * will be fetched from the database. This can
	 * be used when adding new posts.
	 * 
	 * @param Integer $id The ID of the post to populate
	 * the instance of the class with
	 */
	public function __construct($id)
	{
		if ($id != 0) //existing post
		{
			$query = sprintf("SELECT * FROM `blog_post` WHERE `id`='%s' LIMIT 0,1",
				mysql_real_escape_string(strip_slashes($id)));
			$result = mysql_query($query);
			$row = mysql_fetch_array($result);
		
			$this->id = $row['id'];
			$this->title = $row['title'];
			$this->ctime = $row['ctime'];
			$this->mtime = $row['mtime'];
			$this->views = $row['views'];
			$this->content = $row['content'];
		} else { //new post
			$this->id = 0;
		}
	}
	
	/**
	 * Returns the unique identifier for the post.
	 * 
	 * @return Integer The post's unique identifier
	 */
	public function theID()
	{
		return $this->id;
	}
	
	/**
	 * Returns the title of the post.
	 * 
	 * @return String The post's title
	 */
	public function theTitle()
	{
		return $this->title;
	}
	
	/**
	 * Returns the time the post was created.
	 * 
	 * @return String The post's creation time
	 */
	public function theCreationTime()
	{
		return $this->ctime;
	}
	
	/**
	 * Returns the time the post was last modified.
	 * 
	 * @return String The post's last modification time
	 */
	public function theLastModifiedTime()
	{
		return $this->mtime;
	}
	
	/**
	 * Returns the number of views the post has had.
	 * 
	 * @return Integer The post's view count
	 */
	public function theNumberOfViews()
	{
		return $this->views;
	}
	
	/**
	 * Returns the post's content.
	 * 
	 * @return String The post's content
	 */
	public function theContent()
	{
		return $this->content;
	}
	
	/**
	 * Increments the post's view count and updates the
	 * database. There is no need to call updateDatabase()
	 * after calling this function.
	 */
	public function incrementViewCount()
	{
		$query = sprintf("UPDATE `blog_post` SET `views`=`views`+1 WHERE `id`=%d",
			mysql_real_escape_string(strip_slashes($this->id)));
		++$this->views;
	}
	
	/**
	 * Sets the title of the post.
	 * 
	 * You must call updateDatabase() to update the database
	 * with the new title.
	 * 
	 * @param String The new title of the post
	 */
	public function setTitle($title)
	{
		$this->title = $title;
	}
	
	/**
	 * Sets the content of the post.
	 * 
	 * You must call updateDatabase() to update the database
	 * with the new content.
	 * 
	 * @param String The new content of the post
	 */
	public function setContent($content)
	{
		$this->content = $content;
	}
	
	/**
	 * Updates the database with the values stored in
	 * the object. In the event that this is a new post,
	 * a new row will be inserted in the database.
	 */
	public function updateDatabase()
	{
		if ( $this->id == 0 )
		{ //creating a new post
			$query = sprintf("INSERT INTO `blog_post` (`title`, `mtime`, `content`) VALUES ('%s',NOW(),'%s')",
				mysql_real_escape_string(stripslashes($this->title)),
				mysql_real_escape_string(stripslashes($this->content)));
			mysql_query($query);
		} else { //updating an existing post
			$query = sprintf("UPDATE `blog_post` SET `title`='%s', `mtime`=NOW(), `content`='%s' WHERE `id`='%s'",
				mysql_real_escape_string(stripslashes($this->title)),
				mysql_real_escape_string(stripslashes($this->content)));
			mysql_query($query);
		}
	}
}